package com.example.demo.client;

import com.example.demo.entity.deepseek.DeepSeekRequest;
import com.example.demo.entity.deepseek.DeepSeekResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

public class DeepSeekWebClient {
    private static final String API_URL = "https://api.deepseek.com/chat/completions";
    private final String apiKey;
    private final WebClient webClient;

    public DeepSeekWebClient(String apiKey) {
        this.apiKey = apiKey;
        this.webClient = WebClient.builder()
                .baseUrl(API_URL)
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .build();
    }

    public Mono<DeepSeekResponse> chatCompletion(DeepSeekRequest.Message[] messages) {
        DeepSeekRequest request = new DeepSeekRequest();
        request.setModel("deepseek-chat");
        request.setStream(false);
        request.setMessages(Arrays.asList(messages));

        return webClient.post()
                .header("Authorization", "Bearer " + apiKey)
                .bodyValue(request)
                .retrieve()
                .bodyToMono(DeepSeekResponse.class);
    }
}
